package com.demomaster.quickreader;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.demomaster.quick_reader.ReadActivity;
import com.demomaster.quick_reader.constant.Constant;
import com.demomaster.quick_reader.model.novel.CollBookBean;
import com.demomaster.quick_reader.util.MD5Utils;
import com.demomaster.quick_reader.util.PermissionsChecker;
import com.demomaster.quick_reader.util.StringUtils;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private static final int PERMISSIONS_REQUEST_STORAGE = 1;
    static final String[] PERMISSIONS = {
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private PermissionsChecker mPermissionsChecker;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    if (!Environment.isExternalStorageManager()) {
                        Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent);
                    }
                }
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                    if (mPermissionsChecker == null) {
                        mPermissionsChecker = new PermissionsChecker(MainActivity.this);
                    }

                    //获取读取和写入SD卡的权限
                    if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
                        //请求权限
                        ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, PERMISSIONS_REQUEST_STORAGE);
                        return;
                    }
                }


                File file = new File("/storage/emulated/0/authorapp/book/备份/第一章 青山镇.txt");
                CollBookBean collBook = new CollBookBean();
                collBook.set_id(MD5Utils.strToMd5By16(file.getAbsolutePath()));
                collBook.setTitle(file.getName().replace(".txt",""));
                collBook.setAuthor("");
                collBook.setShortIntro("无");
                collBook.setCover(file.getAbsolutePath());
                collBook.setLocal(true);
                collBook.setLastChapter("开始阅读");
                collBook.setUpdated(StringUtils.dateConvert(file.lastModified(), Constant.FORMAT_BOOK_DATE));
                collBook.setLastRead(StringUtils.
                        dateConvert(System.currentTimeMillis(), Constant.FORMAT_BOOK_DATE));
                ReadActivity.startActivity(MainActivity.this,
                        collBook, false);
            }
        });
    }
}
